<?php

namespace app\modules\admin\controllers;

use Yii;
use common\models\SysRole;
use common\models\SysRole2source;
//use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\Tools;

/**
 * SysRoleController implements the CRUD actions for SysRole model.
 */
class SysRoleController extends \app\components\AdminController
{
    public $enableCsrfValidation=false;
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
            ],
        ];
    }

    /**
     * admin/sys-role/index
     * Lists all SysRole models.
     * @return mixed
     */
    public function actionIndex()
    {
        $page_no = Tools::GetSafeParam('page_no', 1, 0);
        $condition = [];
        $condition['name'] = Tools::GetSafeParam('name', '', 1);
        
        $data = SysRole::getRoleList($condition,$page_no);
        
        $page = Tools::showCmPager($page_no, $data['perpage'], $data['total'],$condition);
        
        return $this->render('index',[
            'data' => $data,
            'page' => $page,
            'condition' => $condition,
        ]);
    }

    /**
     * admin/sys-role/view
     * Displays a single SysRole model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * admin/sys-role/create
     * Creates a new SysRole model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $name = Tools::GetSafeParam('name', '', 1);
        if($name != ""){
            $model = new SysRole();
            $model->name = $name;
            $model->addtime = time();
            if($model->save()){
                return $this->redirect(['index']);
            }
        }

        return $this->render('create', [
        ]);
    }

    /**
     * admin/sys-role/update
     * Updates an existing SysRole model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate()
    {
        $name = Tools::GetSafeParam('name', '', 1);
        $id = Tools::GetSafeParam('id', 0, 0);
        $model = $this->findModel($id);
        if ($name != "") {
            $model->name = $name;
            if($model->save()){
                return $this->redirect(['index']);
            }
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * admin/sys-role/delete
     * Deletes an existing SysRole model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        if($this->findModel($id)->delete()){
            $output['success'] = 1;
            $output['msg'] = "删除成功！";
            Tools::outputjson($output);
        }
        else{
            $output['success'] = 0;
            $output['msg'] = "删除失败！";
            Tools::outputjson($output);
        }
    }
    
    
        /***
     * 用户权限编辑
     * admin/sys-role/role-auth
     */
    public function actionRoleAuth(){
        $role_id = \common\models\Tools::GetSafeParam('roleid', 0, 0);
        //查看系统所有资源树
        $sources = \common\models\SysSource::getSourceTree($role_id);
//        echo "<pre>";
//        print_r($sources);
//        
//        exit;
        return $this->render('role-auth', [
            'role_id' => $role_id,
            'sources' => $sources,
        ]);
    }
    
    /***
     * 用户权限编辑保存
     * admin/sys-role/save-role-auth
     */
    public function actionSaveRoleAuth(){
        $role_id = \common\models\Tools::GetSafeParam('role_id', 0, 0);
        
        //删除原先的权限配置
        
        SysRole2source::deleteAll(['roleid'=>$role_id]);
        
        $post = Yii::$app->request->post();
        foreach ($post as $value){
            $power_array = explode('_', $value);
            if($power_array[0] == 'power'){
                $model = new SysRole2source();
                $model->roleid = $role_id;
                $model->sourceid = $power_array[1];
                $model->addtime = time();
                if(!$model->save()){
                    print_r($model->errors);exit;
                }
            }
        }
        return $this->redirect('/admin/sys-role/index');
    }
    
    /**
     * Finds the SysRole model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return SysRole the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = SysRole::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }
}
